package com.javasm.maimaibao.login.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.javasm.common.utils.SmsUtil;
import com.javasm.maimaibao.login.entity.AdminUser;
import com.javasm.maimaibao.login.service.AdminUserService;
import com.javasm.maimaibao.login.service.LoginUserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user/")

public class UserController {
    @Resource
    LoginUserService loginUserService;

    // 测试登录，浏览器访问： http://localhost:8081/user/doLogin?username=zhang&password=123456
    @RequestMapping("doLogin")
    public SaResult doLogin(String username, String password) {
        // 第一步：比对前端提交的账号名称、密码
//            if("zhang".equals(name) && "123456".equals(pwd)) {
//                // 第二步：根据账号id，进行登录
//                StpUtil.login(10001);
//                return SaResult.ok("登录成功");
//            }
//            return SaResult.error("登录失败");
        AdminUser adminUser = loginUserService.doLogin(username, password);
        StpUtil.login(adminUser.getUid());
        return new SaResult(200, "登陆成功", adminUser);
    }

    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
    @RequestMapping("isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }

    @RequestMapping("/phone")
    public SaResult phoneLogin(String code, String phone) {

        AdminUser adminUser = loginUserService.phoneLogine(code, phone);

        return new SaResult(200, "登陆成功", adminUser);
    }

    @RequestMapping("/email")
    public SaResult phoneEmail(String code, String email) {

        AdminUser adminUser = loginUserService.emailLogine(code, email);

        return new SaResult(200, "登陆成功", adminUser);
    }
    @GetMapping("/smsSend")
    public SaResult sendMsg(String phone){
        loginUserService.sendMsg(phone);
           return SaResult.ok();

    }
}

